package com.jianjian.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jianjian.common.result.Result;
import com.jianjian.model.system.SysMenu;
import com.jianjian.model.vo.AssginMenuVo;
import com.jianjian.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author cool
 * @since 2022-11-07
 */
@Slf4j
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "菜单列表(树形架构)")
    @GetMapping("findMenuNodes")
    public Result findMenuNodes(){

       List<SysMenu> list =  sysMenuService.findMenuNodes();
        return Result.ok(list);
    }
    //------------------中间表--------------------------
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
       List<SysMenu> list = sysMenuService.findMenuByRoleId(roleId);
       return Result.ok(list);
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }
    //--------------------------------------------

    @ApiOperation(value = "添加菜单")
    @PostMapping("addMenu")
    public Result addMenu(@ApiParam(name = "sysMenu", value = "菜单对象", required = true)
            @RequestBody(required = true) SysMenu sysMenu){
        if (StringUtils.isEmpty(sysMenu.getName())){
            return Result.fail("添加失败，请输入菜单名称");
        }
        boolean b = sysMenuService.save(sysMenu);
        return b ? Result.ok("添加成功") : Result.fail("添加失败");
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("removeMenu/{id}")
    public Result removeMenu(@ApiParam(name = "id", value = "菜单id", required = true)
                          @PathVariable(required = true) Long id){
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        Integer count = sysMenuService.count(wrapper.eq("parent_id", id));
        if (count > 0 ){
            return Result.fail("删除失败，其菜单下有子菜单");
        }
        sysMenuService.removeById(id);
        return Result.ok("删除成功");
    }

    @ApiOperation(value = "根据id查询菜单")
    @GetMapping("findMenu/{id}")
    public Result findMenu(@ApiParam(name = "id", value = "菜单id", required = true)
                             @PathVariable(required = true) Long id){

        return Result.ok(sysMenuService.getById(id));
    }

    @ApiOperation(value = "修改菜单")
    @PostMapping("updateMenuById")
    public Result updateMenuById(@ApiParam(name = "sysMenu", value = "菜单对象", required = true)
                             @RequestBody(required = true) SysMenu sysMenu){

        boolean b = sysMenuService.updateById(sysMenu);

        return b ? Result.ok("修改成功") : Result.fail("修改失败");
    }


}

